package com.cgnpc.scp.manager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cgnpc.cuddemo.utils.IDHelper;
import com.cgnpc.framework.userinfo.CurrentUser;
import com.cgnpc.scp.common.bean.MyPageResult;
import com.cgnpc.scp.common.pojo.UserInfo;
import com.cgnpc.scp.common.utils.MyPageResultUtil;
import com.cgnpc.scp.common.utils.SqlUtil;
import com.cgnpc.scp.manager.mybatis.entity.Systeminfo;
import com.cgnpc.scp.manager.mybatis.entity.UsualPhraseConfig;
import com.cgnpc.scp.manager.mybatis.mapper.client.UsualPhraseConfigMapper;
import com.cgnpc.scp.manager.service.UsualPhraseConfigService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UsualPhraseConfigServiceImpl implements UsualPhraseConfigService
{
    @Autowired
    private CurrentUser currentUser;

    @Autowired
    private UsualPhraseConfigMapper usualPhraseConfigMapper;

    @Override
    public UsualPhraseConfig save(UsualPhraseConfig config) {
        UserInfo userinfo = currentUser.getCurrentUserDetail();
        if (config.getId() == null) {
            config.setId(IDHelper.uuid());
            config.setCreateUser(userinfo.getUserId());
            config.setCreateUserName(userinfo.getUserName());

            usualPhraseConfigMapper.insert(config);
        }else {
            config.setLastUpdateTime(new Date());
            config.setLastUpdateUser(userinfo.getUserId());
            config.setLastUpdateUserName(userinfo.getUserName());

            usualPhraseConfigMapper.updateById(config);
        }

        return config;
    }

    @Override
    public UsualPhraseConfig getById(String id) {
        return usualPhraseConfigMapper.selectById(id);
    }

    @Override
    public MyPageResult<UsualPhraseConfig> queryPageList(UsualPhraseConfig req) {
        MyPageResult<UsualPhraseConfig> re = null;

        int pageNum = req.getPageNum();
        int pageSize = req.getPageSize();

        com.baomidou.mybatisplus.extension.plugins.pagination.Page<UsualPhraseConfig> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(pageNum, pageSize);


        QueryWrapper<UsualPhraseConfig> queryWrapper = new QueryWrapper<>();
        if (req.getContent()!=null && req.getContent().trim().length()>0) {
            queryWrapper.like("content",req.getContent());
        }
        if (req.getValid() != null) {
            queryWrapper.eq("valid",req.getValid());
        }
        // pagehelper分页插件的问题，需要创建PageInfo才能获取总记录数，queryPage.getResult()和dbDataDictList是一样的
        IPage<UsualPhraseConfig> processInfoIPage = usualPhraseConfigMapper.selectPage(page, queryWrapper);
        // 执行查询
//        List<T> dbEntityList = (List<T>) dataDictMapper.selectByExample(queryExample);
        MyPageResult<UsualPhraseConfig> myPageResult = MyPageResultUtil.createMyPageResultPlus(processInfoIPage);
        return myPageResult;
    }

    @Override
    public boolean delByIds(List<String> ids) {
        boolean re = false;
        usualPhraseConfigMapper.deleteBatchIds(ids);
        re = true;

        return re;
    }
}
